from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://172.16.0.137/shop/index.php?controller=simple&action=login')
# 操作用户名输入框
username_loc = (By.NAME,'login_info')
username = WebDriverWait(driver,10).until(EC.presence_of_element_located(username_loc))
username.send_keys('ceshi2001')
# 操作密码输入框
password_loc = (By.NAME,'password')
password = WebDriverWait(driver,10).until(EC.presence_of_element_located(password_loc))
password.send_keys('ceshi2001')
# 操作登录按钮
submit_loc = (By.CLASS_NAME,'submit_login')
submit = WebDriverWait(driver,10).until(EC.presence_of_element_located(submit_loc))
submit.click()
# 断言
expected_url = 'http://172.16.0.137/shop/index.php?controller=ucenter&action=index'
ret_url = driver.current_url
try:
    ret_loc = (By.CLASS_NAME,'f14')
    ret = WebDriverWait(driver,10).until(EC.presence_of_element_located(ret_loc))
    print('登录成功') if ret_url == expected_url and ret.text == '您好，ceshi2001 欢迎回来!' else print('登录失败')
except:
    print('由于用户名或密码错误，导致登录失败')
sleep(5)
driver.quit()


